﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Repository;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_OrderHistory")]
    public class T_OrderHistory : IEntity<T_OrderHistory>
    {
        public int Id { get; set; }
        public int OrderId { get; set; }
        public DateTime CreateTime { get; set; }
        public int PreStatus { get; set; }
        public int CurrentStatue { get; set; }
        /// <summary>
        /// 支付的时候备注什么支付方式 【货到付款、在线支付】
        /// 管理员确认收货过后可以写备注
        /// </summary>
        [Column(TypeName="nvarchar")]
        [StringLength(256)]
        public string Remark { get; set; }

        public override T_OrderHistory Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_OrderHistory Find(System.Linq.Expressions.Expression<Func<T_OrderHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.OrderHistory.FirstOrDefault(where);
                if (item != null)
                {
                    this.CreateTime = item.CreateTime;
                    this.CurrentStatue = item.CurrentStatue;
                    this.Id = item.Id;
                    this.OrderId = item.OrderId;
                    this.PreStatus = item.PreStatus;
                    this.Remark = item.Remark;
                    return this;
                }
                return null;
            }
        }

        public override T_OrderHistory FindLast(System.Linq.Expressions.Expression<Func<T_OrderHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.OrderHistory.OrderByDescending(c=>c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.CreateTime = item.CreateTime;
                    this.CurrentStatue = item.CurrentStatue;
                    this.Id = item.Id;
                    this.OrderId = item.OrderId;
                    this.PreStatus = item.PreStatus;
                    this.Remark = item.Remark;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_OrderHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.OrderHistory.Any(where);
            }
        }
    }
}
